package com.e9ab98e991ab.moduler.ui

import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout

import com.e9ab98e991ab.libcommon.base.BaseActivity

import androidx.recyclerview.widget.GridLayoutManager
import com.apkfuns.logutils.LogUtils
import com.e9ab98e991ab.libcommon.base.recycle.BaseRecyclerAdapter
import com.e9ab98e991ab.libcommon.base.recycle.SmartViewHolder
import com.e9ab98e991ab.libcommon.library.waterripple.WaterRippleView
import com.e9ab98e991ab.libcommon.utils.toast.ToastyUtils
import com.e9ab98e991ab.moduler.R
import com.e9ab98e991ab.moduler.content.PageContent
import com.e9ab98e991ab.moduler.model.MainGlideModel
import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.toast 


class MainActivity : BaseActivity(), View.OnClickListener {

    private var firstTime: Long = 0
    lateinit var mAdapter: BaseRecyclerAdapter<MainGlideModel>

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    /***
     * 设置区域
     */
    override fun initSet() {
        mainRecyclerView.setLayoutManager(GridLayoutManager(this, 4))
        mainRecyclerView.setItemAnimator(SlideInLeftAnimator())

//        mainRefreshLayout.setEnableOverScrollDrag(true)
    //        mainRefreshLayout.setEnableRefresh(false)
    //        mainRefreshLayout.setEnableLoadMore(false)

    }

    /***
     * 数据绑定
     */
    override fun initDate() {
        mAdapter = object : BaseRecyclerAdapter<MainGlideModel>(PageContent().setPage(), R.layout.list_grid_adapter) {
            override fun onBindViewHolder(holder: SmartViewHolder, model: MainGlideModel, position: Int) {
                holder.image(R.id.lgaIv, model.image)
                holder.text(R.id.lgaTv, getString(model.name))
            }
        }
        mainRecyclerView.adapter = mAdapter
        initListener()
    }

    private fun initListener() {
        mAdapter.setOnItemClickListener { _, _, position, _ ->
            val intent = PageContent().getActivty(this)
            if (intent.size > position) {
                startActivity(intent.get(position))
            } else {
                toast(getString(R.string.to_be_developed))
            }
        }

    }


    override fun onClick(v: View) {
        LogUtils.d(v.id.toString())
    }


    override fun onBackPressed() {
        val secondTime = System.currentTimeMillis()//以毫秒为单位
        if (secondTime - firstTime > 1000) {
            ToastyUtils.info(getString(R.string.exit_program_msg))
            firstTime = secondTime
        } else {
            finish()
            System.exit(0)
        }
    }
}
